﻿namespace TomatoBreak.Infrastructure
{
    using System;

    using Castle.Core;
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.SubSystems.Configuration;
    using Castle.Windsor;

    public abstract class ServiceInstaller<TService> : IWindsorInstaller
    {
        protected virtual LifestyleType Lifestyle
        {
            get { return LifestyleType.Transient; }
        }

        public virtual void Install(IWindsorContainer container, IConfigurationStore store)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            foreach (var assembly in container.Resolve<IBuildManager>().ApplicationAssemblies)
            {
                container.Register(AllTypes.FromAssembly(assembly)
                                           .BasedOn<TService>()
                                           .If(type => !type.IsAbstract && !type.IsInterface && type.IsPublic)
                                           .Configure(c => c.LifeStyle.Is(Lifestyle)));
            }
        }
    }
}